import createApi from './create-axios'
import { Store } from 'vuex'
import { AxiosPromise, AxiosRequestConfig } from 'axios'
import { RootStateOptions } from '@/store/state'

/**
 * @des 获取手机验证码
 */
interface GetCodeParams {
  code: string
}
export interface getCodeData {
  id: number
  'user_id': number
  ip: string
  phone: string | number
  code: string | number
}
type getCodeFnTypes = (params: GetCodeParams) =>AxiosRequestConfig
const getCode:getCodeFnTypes = (params) => {
  return {
    url: '/code',
    method: 'get',
    params: params
  }
}

/**
 * @des 数据曝光上报
 */
export interface MergeObj {
  'event_id': string
  'type_id': number
  timestamp: number
}
/**
 * @des 数据曝光上报
 */
export interface TrackBaseDataType {
  'url': string
  'session_id': string
  source: string
  'patpat_platform': string
  'is_automated': number
  'device_language': string
}
export type TrackDataArrType =Array<MergeObj>
type trackDataTypes = (params: TrackDataArrType) =>AxiosRequestConfig
const trackData:trackDataTypes = (params) => {
  return {
    url: '/track',
    method: 'post',
    data: params
  }
}

type apiresData = getCodeData
type apiConfigtTypes = getCodeFnTypes | trackDataTypes
// type allParams = GetCodeParams | TrackDataArrType
// type reutrnFn = (params: allParams) =>AxiosPromise<apiresData>
type reutrnFn = (params: any) =>AxiosPromise<apiresData>

export interface ApiTypes {
  getCodeFn: reutrnFn
  trackData: reutrnFn
}
const api = (store: Store<RootStateOptions>):ApiTypes => {
  const create = createApi(store)
  const createRequest = (apiConfig: apiConfigtTypes):reutrnFn => {
    return (params) => {
      return create(apiConfig(params))
    }
  }
  return {
    getCodeFn: createRequest(getCode),
    trackData: createRequest(trackData)
  }
}

export default api
